﻿using KPIS.GERP.GAMS.APP.App_UI.Commons;
using KPIS.GERP.GAMS.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ReportRemainCollateral : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        D_BGM_MAS_Source source = new D_BGM_MAS_Source();
        D_ACM_MiscReport rpt = new D_ACM_MiscReport();
        MailMerge mailMerge = new MailMerge();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDateStart.SetValue(DateTime.Today);
                //LoadDDLBgmSourceSeq();
            }
        }

        //protected void LoadDDLBgmSourceSeq()
        //{
        //    ddlBgmSourceSeq.DataSource = source.GetList();
        //    ddlBgmSourceSeq.DataBind();

        //    main.InsertEmptyDDLAll(ddlBgmSourceSeq);
        //}

        protected void btnReport_Click(object sender, EventArgs e)
        {
            string strReportHeader = "";
            //string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            DateTime? dtStartDate = txtDateStart.GetDateValue();

            if (!dtStartDate.HasValue)
            {
                main.MessageAlert(Page, "กรุณาระบุวันที่");
                return;
            }

            DataTable dtb = rpt.ReportRemainCollateral(dtStartDate.Value);
            Dictionary<string, DataTable> dic = new Dictionary<string, DataTable>();
            dic.Add("A6", dtb);

            Dictionary<string, string> dic2 = new Dictionary<string, string>();
            strReportHeader = string.Format("ณ วันที่ {0}", dtStartDate.Value.ToLongDateString());
            dic2.Add("A4", strReportHeader);
            dic2.Add("A1", DateTime.Today.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());

            double dAmount = 0;

            foreach (DataRow dr in dtb.Rows)
            {
                dAmount += main.ConvertNullDouble(dr["total"]) ?? 0;
            }

            int nRow = dtb.Rows.Count + 6;
            dic2.Add("B" + nRow, "รวม");
            dic2.Add("I" + nRow, dAmount.ToString("#,##0.00"));

            mailMerge.MergeExcel(Response, Server.MapPath("~/Reports/Templates/RemainCollateral.xlsx"), string.Format("RemainCollateral-{0}", DateTime.Now.ToString("yyyyMMddHHmmssffff")), dic, dic2);
        }
    }
}